package com.tjakus.bean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ValueChangeEvent;
import com.tjakus.hibernate.HibernateUtil;
import com.tjakus.hibernate.Services;
import com.tjakus.hibernate.Users;
import com.tjakus.logging.Logging;

public class MappingServicesBean {

	private List<Services> servicesListPerUser;
	private String selectedEmail;
	private List<String> emailList = this.init();

	public List<String> init() {
		Logging.getLogger().info("INITIALIZATION!!!");
		List<String> emailList2 = new ArrayList<>();
		List<Users> usersList = HibernateUtil.getAllUsers();
		for (Iterator<Users> iter = usersList.iterator(); iter
				.hasNext();) {
			emailList2.add(iter.next().getEmail());
		}
		return emailList2;
	}
	
	public List<String> getEmailList() {
		return emailList;
	}

	public void setEmailList(List<String> emailList) {
		this.emailList = emailList;
	}
	
	public String getSelectedEmail() {
		return selectedEmail;
	}
	public void setSelectedEmail(String selectedEmail) {
		this.selectedEmail = selectedEmail;
	}

	public List<Services> getServicesListPerUser() {
		return servicesListPerUser;
	}
	public void setServicesListPerUser(List<Services> servicesListPerUser) {
		this.servicesListPerUser = servicesListPerUser;
	}
	
	public void emailChange(ValueChangeEvent event) {
		Logging.getLogger().info("DISPLAY SERVICES: " + event.getNewValue().toString());
		selectedEmail = (String) event.getNewValue();
		List<Integer> serviceIdList = HibernateUtil.getMappingServiceIdsFromUser(selectedEmail);
		setServicesListPerUser(HibernateUtil.getServicesByServiceIds(serviceIdList));
	}

}
